package com.joint.web.action.mp;

import com.fz.us.base.bean.BaseEnum;
import com.fz.us.base.bean.Pager;
import com.fz.us.base.util.DataUtil;
import com.fz.us.base.util.LogUtil;
import com.google.common.collect.Maps;
import com.joint.base.entity.Company;
import com.joint.base.service.UsersService;
import com.joint.base.util.StringUtils;
import com.joint.core.entity.ProInfo;
import com.joint.core.entity.ProLeave;
import com.joint.core.entity.ProSignIn;
import com.joint.core.service.ProInfoService;
import com.joint.core.service.ProLeaveService;
import com.joint.core.service.ProSignInService;
import com.joint.web.action.BaseController;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.*;

/**
 * Created by xh on 2015-3-15.
 */
@Controller("mpCalendar")
@RequestMapping("/mp")
public class AjaxCalendarAction extends BaseController {

    @Resource
    private ProSignInService proSignInService;
    @Resource
    private ProInfoService proInfoService;
    @Resource
    private ProLeaveService proLeaveService;
    @Resource
    private UsersService usersService;

    private ProSignIn proSignIn;
    private String month;
    private Date dateE;
    private Date dateS;
    private int preNext;	//-1/1
    private Date start;
    private String proInfoId;

    @RequestMapping(value = "/ajax-calendar/listCalendarView", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> listCalendarView(@RequestParam("month") String month,@RequestParam("proInfoId") String proInfoId) throws ParseException {

        dateS = DataUtil.StringToDate(month);
        dateE = DataUtil.getAfterMonths(dateS, 1);

        Company com = usersService.getCompanyByUser();

        Map rmap = Maps.newHashMap();
        rmap.put("state",BaseEnum.StateEnum.Enable);
        if (StringUtils.isNotEmpty(proInfoId)){
            ProInfo proInfo = proInfoService.get(proInfoId);
            rmap.put("proInfo",proInfo);

        }
        List<ProSignIn> proSignInList = (List<ProSignIn>)proSignInService.findByPagerAndCompany(
                new Pager(0),"createDate",dateS,dateE,null,com,rmap).getList();

        List<ProLeave> proLeaveList = (List<ProLeave>)proLeaveService.findByPagerAndCompany(
                new Pager(0),"leaveDate",dateS,dateE,null,com,rmap).getList();

        LogUtil.info("{proSignInList}" + proSignInList.size());
        LogUtil.info("{proLeaveList}" + proLeaveList.size());

        List<JSONObject> dataRows = new ArrayList<JSONObject>();
        Map<String, Object> data = new HashMap<String, Object>();

        String summary;
        String url;
        for(ProSignIn proSignIn : proSignInList){
            summary = "";
            Map jsonMap = new HashMap();
            if (StringUtils.isEmpty(proInfoId)){
                summary += "[" + proSignIn.getProInfo().getName() + "] ";
            }
            summary += proSignIn.getCreater().getName();
            if(proSignIn.getStatus()==1){
                summary += " 正常";
            }else{
                summary += " 迟到";
            }

            jsonMap.put("summary", summary);
            jsonMap.put("begin", proSignIn.getCreateDate());
            jsonMap.put("end", proSignIn.getCreateDate());

            url = getPath()+"mp/ajax-prosignin/read/keyId/"+ proSignIn.getId();
            jsonMap.put("url", url);
            dataRows.add(JSONObject.fromObject(jsonMap));
        }

        for(ProLeave proLeave : proLeaveList){
            summary = "";
            Map jsonMap = new HashMap();
            if (StringUtils.isEmpty(proInfoId)){
                summary += "[" + proLeave.getProInfo().getName() + "] ";
            }
            summary += proLeave.getCreater().getName();
            summary += " 请假";

            jsonMap.put("summary", summary);
            jsonMap.put("begin", proLeave.getLeaveDate());
            jsonMap.put("end", proLeave.getLeaveDate());

            url = getPath()+"mp/ajax-proleave/read/keyId/"+ proLeave.getId();
            jsonMap.put("url", url);
            dataRows.add(JSONObject.fromObject(jsonMap));
        }

        data.put("dataRows", JSONArray.fromObject(dataRows));
        return ajaxHtmlAppResult(1, "", JSONObject.fromObject(data));
    }

    public String getProInfoId() {
        return proInfoId;
    }

    public void setProInfoId(String proInfoId) {
        this.proInfoId = proInfoId;
    }

    public Date getDateE() {
        return dateE;
    }

    public void setDateE(Date dateE) {
        this.dateE = dateE;
    }

    public Date getDateS() {
        return dateS;
    }

    public void setDateS(Date dateS) {
        this.dateS = dateS;
    }

    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public ProSignIn getProSignIn() {
        return proSignIn;
    }

    public void setProSignIn(ProSignIn proSignIn) {
        this.proSignIn = proSignIn;
    }

    public int getPreNext() {
        return preNext;
    }

    public void setPreNext(int preNext) {
        this.preNext = preNext;
    }

    public Date getStart() {
        return start;
    }

    public void setStart(Date start) {
        this.start = start;
    }
}
